#ifndef _DICOM_UKEY_UKEY_H_
#define _DICOM_UKEY_UKEY_H_

#include <gwbase/byte_array.h>
#include <gwbase/base/status.h>

namespace ukey {

using namespace dicom;

struct TokenInfo {
  uint32_t slot_id;
  std::string label;
  std::string serial_number;
};

class UKeyHelper {
 public:
  UKeyHelper(void);
  virtual ~UKeyHelper(void);

  Status GetTokenList(std::vector<TokenInfo> *list);

  Status FormatToken(uint32_t slot_id, const std::string &token_name,
                     const std::string &sopin, const std::string &pin);

  // 检查PIN是否正确
  Status CheckPin(uint32_t slot_id, const std::string &pin);

  // 输入6次错误PIN之后，UKey锁定，必须解锁
  Status UnlockPin(uint32_t slot_id, const std::string &sopin,
                   const std::string &new_pin);

  // 修改PIN
  Status SetTokenPin(uint32_t slot_id, const std::string &oldpin,
                     const std::string &pin);

  // 清除UKey中的所有和SurDoc相关的数据，即删除所有满足如下条件的数据
  // CKO_DATA.label == "SurDoc#Account"
  // CKO_DATA.label == "SurDoc#PriKeyCipher"
  // CKO_PRIVATE_KEY.subject == "SurDoc#RsaPriKey
  // CKO_PUBLIC_KEY.subject == "SurDoc#RsaPubKey
  Status ClearSurDocCredential(uint32_t slot_id, const std::string &pin);

  Status SaveSurDocCredential(uint32_t slot_id, const std::string &pin,
                              const std::string &account,
                              const ByteArray &credential);

  Status LoadSurDocCredential(uint32_t slot_id, const std::string &pin,
                              std::string *account, ByteArray *credential);

  static Status GlobalInit();
  static Status GlobalCleanup();
};
}

#endif  // _DICOM_UKEY_UKEY_H_
